package com.allwees.bs.module.wms.service.impl;

import com.allwees.bs.module.base.service.impl.BoServiceImpl;
import com.allwees.bs.module.wms.bo.WmsShippingBo;
import com.allwees.bs.module.wms.dao.WmsShippingOrderDao;
import com.allwees.bs.module.wms.entity.WmsPickOrderSku;
import com.allwees.bs.module.wms.entity.WmsShippingOrder;
import com.allwees.bs.module.wms.service.WmsShippingOrderService;
import com.allwees.bs.module.wms.service.WmsShippingOrderSkuService;
import com.allwees.bs.module.wms.wf.WmsShippingOrderAction;
import com.allwees.bs.module.wms.wf.WmsShippingOrderState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * wms 发货订单
 *
 * @author mouhn
 * @since 2020-01-08 13:56
 */
@Service
public class WmsShippingOrderServiceImpl extends BoServiceImpl<WmsShippingOrderDao, WmsShippingOrder> implements WmsShippingOrderService {

    @Autowired
    private WmsShippingOrderSkuService shippingOrderSkuService;

    @Override
    public void create(WmsShippingBo shippingBo) {
        WmsShippingOrder shippingOrder = _gen(shippingBo);
        shippingBo.getPickOrderSkus().forEach(pickOrderSku -> {
            shippingOrderSkuService.create(shippingOrder.getUuid(), pickOrderSku);
        });
    }

    private WmsShippingOrder _gen(WmsShippingBo shippingBo) {
        int quantity = shippingBo.getPickOrderSkus().stream().filter(o -> o.getPickQuantity() != null).mapToInt(WmsPickOrderSku::getPickQuantity).sum();

        WmsShippingOrder shippingOrder = new WmsShippingOrder()
                .setWarehouseUuid(shippingBo.getWarehouseUuid())
                .setExpressCode(shippingBo.getExpressCode())
                .setShippingAt(shippingBo.getShippingAt())
                .setLogisticsCompanyUuid(shippingBo.getLogisticsCompanyUuid())
                .setShippingUserUuid(shippingBo.getShippingUserUuid())
                .setShippingQuantity(quantity);

        shippingOrder.start(WmsShippingOrderState.START, WmsShippingOrderAction.PLATFORM_SHIPPED);

        create(shippingOrder);

        return shippingOrder;
    }
}
